# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name：     container_num_recognition_inference
   Description :
   Author :       'li'
   date：          2021/7/4
-------------------------------------------------
   Change Activity:
                   2021/7/4:
-------------------------------------------------
"""
from ml.modules.container_num_recognition.single_container_recognition.module.detect.num_detector import NumDetector
from ml.modules.container_num_recognition.single_container_recognition.module.gen_res.recognition_result_process import \
    RecognitionResultProcessor
from ml.modules.container_num_recognition.single_container_recognition.module.recognize.num_recognize import NumRecognizer


class ContainerNumRecognitionInference:
    def __init__(self, img, torch_detector=None,
                 torch_recognize_inference=None):
        """
        :param img:
        """
        self.img = img
        self.torch_detector = torch_detector
        self.recognition_result_processor = None
        self.final_result_generator = None
        self.torch_recognize_inference = torch_recognize_inference

    def recognize(self):
        if self.img is None:
            return {}
        self.recognition_result_processor = self._get_recognition_result()
        self.final_result_generator = self.recognition_result_processor.get_final_result()
        return self.final_result_generator

    def get_detection_result(self):
        bboxes_areas_results = NumDetector(img=self.img, torch_detector=self.torch_detector).get_bboxes_areas()
        return bboxes_areas_results

    def _get_recognition_result(self):
        """
        :return:
        """
        result_processor = RecognitionResultProcessor()
        detection_result = self.get_detection_result()
        for res in detection_result:
            area, box = res
            res = NumRecognizer(self.torch_recognize_inference).recognition_container_textarea(area)
            result_processor.add_recognition_result(textarea=area, bbox=box, recognition_content=res)
        return result_processor
